const express = require('express')
const app = express();
const hostname = 'localhost';
const port = 3000;

app.set('views', 'views');
app.set('view engine', 'hbs');


function loggingMiddleware(req, res, next) {
    const time = new Date();
    console.log(`[${time.toLocaleString()}] ${req.method} ${req.url}`);
    next();
  }
app.use(loggingMiddleware)
  
app.use(express.static('public'));

const indexRouter = require('./routes/index');
const apiRouter = require('./routes/api');

app.use('/', indexRouter);
app.use('/api', apiRouter);

app.use('*', (req, res) => {
  res.status(404).render('404', { url: req.originalUrl });
});


app.listen(port, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});